require('./index.scss');
var Tool = require('common/tool');
require('comp/dialog');
var template = require('./template.html');

var comp = avalon.component('ms-enable-account', {
    template: template,
    defaults: {
        accountList:'',
        unEnable:0,                                // 未激活激活的账号在accountList数组里面的索引
        hasEnable:0,                               // 已经激活的账号在accountList数组里面的索引， 通过hasEnable来查找已激活的账号
        enableDialog:{},
        loading:false,
        step:1,
        pwd:'',
        pwdError:'',
        agree:false,
        /* 点击下一步 */
        nextStep(){
            if(this.pwd.length===0){
                this.pwdError = '请输入密码';
                return false;
            }
            this.pwdError = '';
            checkPwd(this);
        },
        /* 确认开户 */
        emmitAcc(){
            if(!this.agree){
                this.pwdError = '请确认已阅读事项！';
                return false;
            }
            this.pwdError = '';
            emmitAccAjax(this);
        },
        onReady:function(e){
            e.vmodel.$watch('enableDialog.display', function(v){
                if( v=='none' ) e.vmodel.step =1;
            });
        }
    }
});

module.exports = comp;


/* 激活账号时验证密码 */
function checkPwd(that){
    that.loading = true;
    Tool.ajax({
        url:'/verificationPasswordAjax',
        type:'post',
        data:{
            password:that.pwd
        }
    }).done(function(res){
        that.pwd = '';
        that.loading = false;
        if( res.code=='0' ){
            that.step = 2;
        }else{
            that.pwdError = res.msg;
        }
    });
}

/* 激活新账户 */
function emmitAccAjax(that){
    that.loading = true;
    console.log('comp.hasEnable',comp.hasEnable);
    var _hasEnable = that.hasEnable;
    var _unEnable = that.unEnable;
    Tool.ajax({
        url:'/activationAccountAjax',
        type:'post',
        data:{
            platform:that.accountList[_hasEnable].platform,
            toPlatform:that.accountList[_unEnable].platform
        }
    }).done(function(res){
        that.loading = false;
        if( res.code=='0' ){
            location.reload();
        }else{
            that.pwdError = res.msg;
        }
    });
}